package com.kaishengit.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.kaishengit.dao.ProjectDao;
import com.kaishengit.dao.Project_UserDao;
import com.kaishengit.entity.Project;
import com.kaishengit.entity.Project_User;
import com.kaishengit.entity.User;

public class MainServlet extends HttpServlet {

	
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		List<Project_User> pulist = new ArrayList<Project_User>();
		List<Project> plist = new ArrayList<Project>();
		Project_UserDao pudao = new Project_UserDao();
		ProjectDao pdao = new ProjectDao();
		pulist = pudao.findByUserId(user.getId());
		if(pulist==null){
			plist = null;
		}	else{
				for(Project_User pu:pulist){
					Project p = new Project();
					p = pdao.findByProjectId(pu.getProjectID());
					plist.add(p);
				}
		}
		request.setAttribute("plist", plist);
		System.out.println(plist.isEmpty());
		RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/views/main.jsp");
		rd.forward(request, response);
	}

}
